﻿using pTools.cMethods;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ChildWin
{
    public partial class YangBanPrint : Form
    {
        DBHelper db;
        string ycode;
        DataTable dt = new DataTable();
        public YangBanPrint(string ycode)
        {
            this.ycode = ycode;
            InitializeComponent();
        }

        private void YangBanPrint_Load(object sender, EventArgs e)
        {
            db = new DBHelper();
            loadPrintTemplte();
            PrintDocument fPrintDocument = new PrintDocument(); //获取默认打印机的方法
            cbPrint.Text = fPrintDocument.PrinterSettings.PrinterName;
            selYangBan();
        }

        private void selYangBan() 
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select yCode,yName,weizhi,shuliang,memo from fun_YangBan_info where yCode='"+ycode+"'");
            var table = db.getData(sb.ToString(), "");
            txt_code.Text = ycode;
            txt_name.Text = table.Rows[0]["yName"].ToString();
            txt_cf.Text = table.Rows[0]["weizhi"].ToString();
            txt_num.Text = table.Rows[0]["shuliang"].ToString();
            txt_memo.Text = table.Rows[0]["memo"].ToString();
            sb = new StringBuilder();
            sb.Append("select proName,spec from V_YangBanProducts_info where yCode='" + ycode + "'");
            dt = db.getData(sb.ToString(), "");

        }

        /// <summary>
        /// 打印模板
        /// </summary>
        private void loadPrintTemplte()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select lbCode,lbName from bar_PrintTemplates_list where status=1");
            // sb.Append(" and lType='" + lType + "'");
            var table = db.getData(sb.ToString(), "");
            cbTemplate.DataSource = table;
            cbTemplate.ValueMember = "lbCode";
            cbTemplate.DisplayMember = "lbName";
        }
        private void printmoban()
        {
            var table = CommonMethods.YangBanPrintTable();
            #region 填充表格数据
            var r = table.NewRow();
            r["yCode"] = txt_code.Text;
            r["yName"] = txt_name.Text;
            r["weizhi"] = txt_cf.Text;
            r["shuliang"] = txt_num.Text;
            r["memo"] = txt_memo.Text;


            for (int i = 0; i < dt.Rows.Count; i++)
            {
                r["name" + i] = dt.Rows[i]["proName"].ToString();
                r["spec" + i] = dt.Rows[i]["spec"].ToString();
            }

            //r["name2"] = txt_zbcj.Text;
            //r["spec2"] = txt_bc.Text;
            //r["name3"] = txt_zbcj.Text;
            //r["spec3"] = txt_bc.Text;
            //r["name4"] = txt_zbcj.Text;
            //r["spec4"] = txt_bc.Text;
            //r["name5"] = txt_zbcj.Text;
            //r["spec5"] = txt_bc.Text;
            //r["name6"] = txt_zbcj.Text;
            //r["spec6"] = txt_bc.Text;
            //r["name7"] = txt_zbcj.Text;
            //r["spec7"] = txt_bc.Text;
            //r["name7"] = txt_zbcj.Text;
            //r["spec7"] = txt_bc.Text;
            table.Rows.Add(r);
            #endregion
            string info = "";
            var pResult = CommonMethods.DoPrint(table, cbTemplate.Text, cbPrint.Text, ref info);
            if (!pResult)
            {
                MessageBox.Show("条码打印失败：" + info, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            printmoban();
        }
    }
}
